package com.steakliu.sun.client;

import com.steakliu.sun.client.api.ClientConfig;
import com.steakliu.sun.proxy.ProxyConfig;
import com.steakliu.sun.proxy.api.ProxyFactory;
import com.steakliu.sun.spi.loader.ExtensionLoader;

/**
 * 功能说明：
 * <p>
 * Original @Author: steakliu-刘牌, 2023-01-08  01:38
 * <p>
 * Copyright (C)2020-2022  steakliu All rights reserved.
 */
public class SunClient {

    private final ClientConfig config;

    public SunClient(ClientConfig config) {
        this.config = config;
    }

    public SunClient(String version,String group,String registryType,String registryAddress,String serializer,String proxy,
                     int timeout,boolean async,boolean oneway, String loadBalance,boolean cache,
                     int cacheTime,int retries,int retryInterval,int heartbeatRetries,
                     int heartbeatInterval,String token,String directConnections, String flowControlType,
                     int concurrentCorePoolSize, int concurrentMaximumPoolSize, String reflectType){
        this.config = ClientConfig.builder()
                .version(version)
                .group(group)
                .registryType(registryType)
                .registryAddress(registryAddress)
                .serializer(serializer)
                .proxy(proxy)
                .timeout(timeout)
                .async(async)
                .oneway(oneway)
                .loadBalance(loadBalance)
                .cache(cache)
                .cacheTime(cacheTime)
                .retries(retries)
                .retryInterval(retryInterval)
                .heartbeatRetries(heartbeatRetries)
                .heartbeatInterval(heartbeatInterval)
                .token(token)
                .directConnections(directConnections)
                .flowControlType(flowControlType)
                .concurrentCorePoolSize(concurrentCorePoolSize)
                .concurrentMaximumPoolSize(concurrentMaximumPoolSize)
                .reflectType(reflectType)
                .build();

    }

    public <T> T create(final Class<T> clazz) throws Exception {
        ProxyFactory proxyFactory = ExtensionLoader.getExtension(ProxyFactory.class, config.getProxy());
        ProxyConfig<T> proxyConfig = new ProxyConfig<>(clazz, config);
        proxyFactory.init(clazz, proxyConfig);
        return proxyFactory.createProxy(clazz);
    }
}
